#!/usr/bin/env bash
. common.sh
ver=1.18.0
if [ "$1" != "" ]; then
    ver=$1
fi
YellowEcho "需要安装的N个inx版本为："$ver
yum install -y wget openssl openssl-devel gcc gcc-g++ perc zlib
wget -c http://nginx.org/download/nginx-$ver.tar.gz
if [ -f "nginx-$ver.tar.gz" ]; then
    tar zxvf nginx-$ver.tar.gz
    cd nginx-$ver
    # 开始进行NGINX安装
    YellowEcho "开始进行Nginx版本"$ver"的安装"
    groupadd www
    useradd -s /sbin/nologin -g www www
    # 设置默认WEB目录
    mkdir -p /home/wwwroot/default
    chown -R www:www /home/wwwroot/default
    # 创建日志目录
    mkdir -p /home/wwwlogs
    touch /home/wwwlogs/nginx_error.log
    touch /home/wwwlogs/access.log
    chown -R www:www /home/wwwlogs
    # 开始编译安装Nginx
    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module
    make && make install
    # 开始配置配置文件
    cat >/usr/lib/systemd/system/nginx.service<<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
EOF
# 开始修改配置文件，以及配置文件路径
rm -rf /usr/local/nginx/conf/nginx.conf
cp -a conf/nginx/*.conf /usr/local/nginx/conf/
mkdir -p /usr/local/nginx/conf/vhost
ln -s /usr/local/nginx/sbin/nginx /usr/bin/
# 开始将nginx设置为开机启动以及启动nginx
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
# 开始开放端口
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https
firewall-cmd --reload
cp -a assets/* /home/wwwroot/default/
else
  exit "Nginx 版本"$ver"下载失败"
fi







